package com.food.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.food.dao.service.CountryService;
import com.food.dao.service.FooddetailService;
import com.food.dao.service.FoodsaveService;
import com.food.dao.service.FoodsearchService;
import com.food.dao.service.MemberService;
import com.food.dao.service.MessageService;
import com.food.dao.service.RecipeService;
import com.food.dao.service.StepService;
import com.food.model.Country;
import com.food.model.Fooddetail;
import com.food.model.Foodsave;
import com.food.model.Foodsearch;
import com.food.model.Member;
import com.food.model.Message;
import com.food.model.Recipe;
import com.food.model.Step;

/**
 * Servlet implementation class ShowRecipe
 */
@WebServlet("/recipe/ShowRecipe")
public class ShowRecipe extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		int rid = Integer.parseInt(request.getParameter("rid"));
		//找食譜
		RecipeService service = new RecipeService();
		Recipe recipe = new Recipe();
		recipe.setRid(rid);
		Recipe foundRecipe = service.findRecipeById(recipe);
		//找國籍
		CountryService countryService = new CountryService();
		Country country = new Country();
		country.setCountryid(foundRecipe.getCountryid());
		country = countryService.findCountry(country);
		
		//找食譜食材
		FoodsaveService foodsaveService = new FoodsaveService();
		Foodsave foodsave = new Foodsave();
		foodsave.setRid(rid);
		List<Foodsave> foundFoodsave = foodsaveService.findFoodsaveByRecipe(foodsave);
//		List<Integer> fsids = new ArrayList<Integer>();
//		for(Foodsave fs:foundFoodsave){
//			fsids.add(fs.getFsid());
//		}
		List<Foodsearch> foodsearchList = new ArrayList<Foodsearch>();
		FoodsearchService foodsearchService = new FoodsearchService();
		for(Foodsave fsid:foundFoodsave){
			Foodsearch foodsearch = new Foodsearch();
			foodsearch.setFsid(fsid.getFsid());
			foodsearchList.add(foodsearchService.findFoodsearchByID(foodsearch));
		}
		//找步驟
		StepService stepService = new StepService();
		Step step = new Step();
		step.setRid(rid);
		List<Step> stepList = stepService.findStepByRecipe(step);
		//找食材明細
		FooddetailService fooddetailService = new FooddetailService();
		Fooddetail fooddetail = new Fooddetail();
		fooddetail.setRid(rid);
		List<Fooddetail> fooddetailList = fooddetailService.findFoodDetailByRecipe(fooddetail);
		//寫這食譜的會員
		MemberService memberService = new MemberService();
		Member m = new Member();
		m.setMbid(foundRecipe.getMbid());
		m=memberService.findMember(m); 
		//留言
		MessageService messageService = new MessageService();
		Message message = new Message();
		message.setRid(rid);
		List<Message> messageList = messageService.findMessageByRecipe(message);
		
		//setAttribute
		request.setAttribute("recipe", foundRecipe);
		request.setAttribute("foodsearch", foodsearchList);
		request.setAttribute("step", stepList);
		request.setAttribute("fooddetail", fooddetailList);
		request.setAttribute("writeMember", m);
		request.setAttribute("country", country);
		request.setAttribute("message", messageList);
		
		RequestDispatcher rd = request
				.getRequestDispatcher("/recipe/seerecipe.jsp");
		rd.forward(request, response);
		return;
		
		
	}

}
